﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Options;
using WisDom.Models;
using MySql.Data.EntityFrameworkCore.Extensions;


namespace WisDom.DBContext
{
    public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        private DbContextOption _dbOption;
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IOptions<DbContextOption> dbOptions)
            : base(options)
        {
            this._dbOption = dbOptions.Value;
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            switch (_dbOption.DbType)
            {
                case DbType.MYSQL:
                    optionsBuilder.UseMySQL(_dbOption.ConnectionString);
                    break;

                default:
                    optionsBuilder.UseSqlServer(_dbOption.ConnectionString);
                    break;
            }

            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            new MappingModel(builder).ModelCreating();
        }
        #region 操作方法


        /// <summary>
        /// ExecuteSqlWithNonQuery
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public int ExecuteSqlWithNonQuery(string sql, params object[] parameters)
        {
            return Database.ExecuteSqlCommand(sql, CancellationToken.None, parameters);
        }



        /// <summary>
        /// 更新数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public void Edit<T>(T entity) where T : class
        {
            Entry(entity).State = EntityState.Modified;
        }


        /// <summary>
        /// 更新数据的列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="updateColumns"></param>
        /// <returns></returns>
        public void Update<T>(T model, params string[] updateColumns) where T : class
        {
            if (updateColumns != null && updateColumns.Length > 0)
            {
                if (Entry(model).State == EntityState.Added ||
                    Entry(model).State == EntityState.Detached) Set<T>().Attach(model);
                foreach (var propertyName in updateColumns)
                {
                    Entry(model).Property(propertyName).IsModified = true;
                }
            }
            else
            {
                Entry(model).State = EntityState.Modified;
            }
        }


        #endregion

        public virtual DbSet<UPChartDetail> UPChartDetail
        {
            get;
            set;
        }

        public virtual DbSet<UPChartInfo> UPChartInfo
        {
            get;
            set;
        }

        public virtual DbSet<UPDataSource> UPDataSource
        {
            get;
            set;
        }

    }
}
